package com.kikyou.service;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;

import com.kikyou.entity.ParameterAttri;
import com.kikyou.entity.WechatReply;
import com.kikyou.util.WeChatUrlReques;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**![](C:/Users/18782/Documents/ShareX/Screenshots/2022-08/idea64_2Gjab3oilz.png)
 * @ClassName weatherPushService
 * @Description T0D0
 * @Author ykq11
 * @Date 2022/8/22 20:23
 * @Version 1.0
 **/
@Service
public class weatherPushService {
    @Value("${wechat.touserChendu}")
    private String touserChendu;
    @Value("${wechat.template_id}")
    private String template_id;
    @Autowired
    private WeChatUrlReques weChatUrlReques;

    String chendu="%E6%88%90%E9%83%BD";




    @Scheduled(cron = "0 02 08 * * ? ")
    public void weChatRequeUrlChendu() throws Exception {
        String access_token = weChatUrlReques.getAccess_token();
        String accUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token;
        // 添加请求头信息
        Map<String, String> heads = new HashMap<>();
        // 使用json发送请求，下面的是必须的
        heads.put("Content-Type", "application/json;charset=UTF-8");
         String[] split = touserChendu.split ( "," );
         for ( String item:split ){
             HttpResponse response = HttpRequest.post(accUrl)
                     .headerMap(heads, false)
                     .body(parameter(item,chendu))
                     .timeout(5 * 60 * 1000)
                     .execute();
           //  System.out.println (response);
         }
    }


    //请求携带参数json
    public String parameter(String userName,String city) throws Exception {

        Map<String, Object> map = new HashMap<>();
        //天气获取
        Map<String, Object> weather = weChatUrlReques.getWeather(city);
        ParameterAttri todayDate = ParameterAttri.builder ()
                .color("#ff6680")
                .value((String) weather.get("date"))
                .build ();
        map.put("todayDate", todayDate);
        //打招呼
        ParameterAttri HelloDate = ParameterAttri.builder ()
                .color("#ff6680")
                .value(getWeather(city))
                .build ();
        map.put("HelloDate", HelloDate);
        //封装天气提醒
        ParameterAttri WearchDate = ParameterAttri.builder ()
                .color("#ff6680")
                .value(getDateSx())
                .build ();
        map.put("WearchDate", WearchDate);
        //封装星期
        ParameterAttri WeekDate = ParameterAttri.builder ()
                .color("#e06f1f")
                .value( (String) weather.get("week"))
                .build ();
        map.put("WeekDate", WeekDate);
        //当前时间
        ParameterAttri UpdateTimeDate = ParameterAttri.builder ()
                .color("#e06f1f")
                .value(getTime())
                .build ();
        map.put("UpdateTimeDate", UpdateTimeDate);
        //当前城市
        ParameterAttri CityDate = ParameterAttri.builder ()
                .color("#cc0022")
                .value( (String) weather.get("city"))
                .build ();
        map.put("CityDate", CityDate);
        //今天天气
        ParameterAttri WeaDate = ParameterAttri.builder ()
                .color("#cc0022")
                .value( (String) weather.get("wea"))
                .build ();
        map.put("WeaDate", WeaDate);
        //当前温度
        ParameterAttri TemDate = ParameterAttri.builder ()
                .color("#e06f1f")
                .value( (String) weather.get("tem"))
                .build ();
        map.put("TemDate", TemDate);
        //穿衣指数
        ParameterAttri Cy = ParameterAttri.builder ()
                .color("#e06f1f")
                .value( (String) weather.get("cy"))
                .build ();
        map.put("Cy", Cy);
        //紫外线指数
        ParameterAttri Zy = ParameterAttri.builder ()
                .color("#e06f1f")
                .value( (String) weather.get("zy"))
                .build ();
      //  map.put("Zy", Zy);
        //舒适指数
        ParameterAttri Sh = ParameterAttri.builder ()
                .color("#e06f1f")
                .value( (String) weather.get("sh"))
                .build ();
        map.put("Sh", Sh);
        //空调开启指数
        ParameterAttri Kt = ParameterAttri.builder ()
                .color("#e06f1f")
                .value( (String) weather.get("kt"))
                .build ();
     //   map.put("Kt", Kt);
        //化妆指数
        ParameterAttri Hz = ParameterAttri.builder ()
                .color("#e06f1f")
                .value( (String) weather.get("hz"))
                .build ();
        //map.put("Hz", Hz);
        //情话

        ParameterAttri yiYanDate = ParameterAttri.builder ()
                .color("#e06f1f")
                .value( getQingHuaContent())
                .build ();
        map.put("yiYanDate", yiYanDate);
        //微博热搜
        Map<String, Object> weiboMap = weChatUrlReques.getWeiBoHost();
        ParameterAttri weiboDate = ParameterAttri.builder ()
                .color("#e06f1f")
                .value((String) weiboMap.get("weiboContent"))
                .build ();
      //  map.put("weiboDate", weiboDate);
        WechatReply mapData = WechatReply.builder ()
                .touser ( userName )
                .template_id ( template_id )
                .data(map).build ();
        return JSON.toJSONString(mapData);
    }

    public String getDateSx() {
        String nihao = "";
        Calendar cal = Calendar.getInstance();
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        if (hour >= 6 && hour < 8) {
            nihao = "早呀，早呀！";
        } else if (hour >= 8 && hour < 11) {
            nihao = "早上好哦！";
        } else if (hour >= 11 && hour < 13) {
            nihao = "中午好呀！";
        } else if (hour >= 13 && hour < 17) {
            nihao = "下午好呀！";
        }else if (hour >= 17 && hour < 19) {
            nihao = "下班下班了！";
        }else if (hour >= 19 && hour < 23) {
            nihao = "晚上好哦";
        }
        else {
            nihao = "看了下月色，发现很晚了哦，记得早睡！";
        }
        return nihao;
    }

    public String getWeather(String city) {
        String weatherNotes = "";
        Map<String, Object> weather = weChatUrlReques.getWeather(city);
        String week = (String) weather.get("wea");
        String tem = (String) weather.get("tem");
        if (week.contains("雨")) {
            weatherNotes = "今天可能下雨哦，记得带伞";
        }
        if (week.contains("云")) {
            weatherNotes = "今天天气还不错嘛";
        }
        if (week.contains("晴")) {
            weatherNotes = "今天可能有点热哦";
        }
        if (week.contains("阴")) {
            weatherNotes = "今天天气很凉爽";
        }
        if(Integer.parseInt(tem)>38){
            weatherNotes = "哇塞，今天好热，千万注意别中暑了哦";
        }
        if(Integer.parseInt(tem)<20){
            weatherNotes = "真冷啊，注意保暖";
        }
        return weatherNotes;
    }

public  String getQingHuaContent() {
        String qinghuaStr = "";
        String dateSx = getDateSx();
    Map<String, String> qingHuaMap = WeChatUrlReques.QING_HUA_MAP;
     if(dateSx.contains("早")){
        qinghuaStr= qingHuaMap.get("早");
        }else if(dateSx.contains("中")) {
         qinghuaStr= qingHuaMap.get("中");
        }else if (dateSx.contains("下")){
         qinghuaStr= qingHuaMap.get("下");
      }else if (dateSx.contains("晚")){
         qinghuaStr= qingHuaMap.get("晚");
     }

       return qinghuaStr;
     }


     public String getTime() {
         Calendar calendar= Calendar.getInstance();
         SimpleDateFormat dateFormat= new SimpleDateFormat("hh:mm");
         return dateFormat.format(calendar.getTime());
     }
}
